Learn R Programming

cppcontainers (version 1.0.4)

[,CppMap-method: Access or insert elements without bounds checking

Description

Read or insert a value by key in a CppMap or CppUnorderedMap. Read a value by index in a CppVector or CppDeque.

Usage

# S4 method for CppMap
[(x, i)

# S4 method for CppUnorderedMap [(x, i)

# S4 method for CppVector [(x, i)

# S4 method for CppDeque [(x, i)

Value

Returns the value associated with i.

Arguments

x

A CppMap, CppUnorderedMap, CppVector, or CppDeque object.

i

A key (CppMap, CppUnorderedMap) or index (CppVector, CppDeque).

Details

In the two associative container types (CppMap, CppUnorderedMap), [] accesses a value by its key. If the key does not exist, it enters the key with a default value into the container. The default value is 0 for integer and double, an empty string for string, and FALSE for boolean.

In the two sequence container types (CppVector, CppDeque), [] accesses a value by its index. If the index is outside the container, this crashes the program.

at and [] both access elements. Unlike [], at checks the bounds of the container and throws an error, if the element does not exist.

See Also

at, back, contains, front, top.

Examples

Run this code
m <- cpp_map(4:6, seq.int(0, 1, by = 0.5))
m
# [4,0] [5,0.5] [6,1]

m[6L]
# [1] 1

m
# [4,0] [5,0.5] [6,1] 

m[8L]
# [1] 0

m
# [4,0] [5,0.5] [6,1] [8,0]

v <- cpp_vector(4:6)
v
# 4 5 6

v[1L]
# [1] 4

v
# 4 5 6

Run the code above in your browser using DataLab